package com.byxy.computer.student.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.byxy.computer.student.entity.Student;

/**
 * @author Daniel
 * @E-Mail iva0512@gmail.com
 * @Date 2016年9月25日
 */
public class Html5Intercepter extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		// System.out.println(request.getRequestURI());

		String uri = request.getRequestURI();

		if (uri.contains("student")) {
			Student u = (Student) request.getSession().getAttribute("student");
			if (u == null) {
//				if (request.getHeader("x-requested-with") != null
//						&& request.getHeader("x-requested-with").equals("XMLHttpRequest")) { // ajax请求
//					response.setHeader("sessionstatus", "timeout");
//					response.getWriter().print("timeout"); //打印一个返回值
//				}
				response.sendRedirect(request.getContextPath() + "/html5/student/login.html");
				return false;
			}
		} else if (uri.contains("teacher")) {
			String admin = (String) request.getSession().getAttribute("admin");
			if (admin == null) {
//				if (request.getHeader("x-requested-with") != null
//						&& request.getHeader("x-requested-with").equals("XMLHttpRequest")) { // ajax请求
//					response.setHeader("sessionstatus", "timeout");
//				}
				response.sendRedirect(request.getContextPath() + "/html5/teacher/login.html");
				return false;
			}
		}

		return super.preHandle(request, response, handler);
	}
}
